package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 添加
     *
     * @param name
     * @param age
     * @param sex
     * @return
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult add(@PathVariable String name, @PathVariable Integer age, @PathVariable String sex) {
        int row = reviewService.add(name, age, sex);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 添加
     *
     * @param review
     * @return
     */
    @PostMapping("/saveData")
    public AjaxResult add(@RequestBody Review review) {
        int row = reviewService.addReview(review);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改
     *
     * @param review
     * @return
     */
    @DeleteMapping("/update")
    public AjaxResult update(@RequestBody Review review) {
        int row = reviewService.update(review);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        int row = reviewService.deleteById(id);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public AjaxResult findById(Integer id){
        Review review = reviewService.findById(id);
        return AjaxResult.success(review);
    }
    @GetMapping("/getDataByPage")
        public TableDataInfo findPage(int pageNum, int pageSize){

        TableDataInfo tableDataInfo = reviewService.findPage(pageNum, pageSize);
        return tableDataInfo;
    }
}


